/*
 * Copyright (c) 2016 Qualcomm Technologies, Inc.
 * All Rights Reserved.
 * Qualcomm Technologies, Inc. Confidential and Proprietary.
 */
package com.thunder.rebot.rebotclient.vam.ui;

import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;

import com.thunder.rebot.rebotclient.R;
import com.thunder.rebot.rebotclient.vam.Algo;
import com.thunder.rebot.rebotclient.vam.entity.FaceFrame;
import com.thunder.rebot.rebotclient.vam.entity.MapperParams;
import com.thunder.rebot.rebotclient.vam.utils.Utils;

import java.util.ArrayList;

/**
 * Class for handling the FR frames
 */

/* package */ class FrFramesUI {
    private static final String TAG = FrFramesUI.class.getSimpleName();

    private final Drawable parent;

    private ArrayList<FaceFrame> frames = new ArrayList<FaceFrame>();
    private final Paint framePaint;
    private final Paint bannerBasePaint;
    private final Paint bannerTextPaint;

    private final float bannerHt;
    private final float textOffX;
    private final float textY;

//    private Bitmap bmAdd;


    /**
     * Constructor for the class
     *
     * @param resources activity resources
     * @param parent    drawable parent
     */
    /* package */ FrFramesUI(Resources resources, Drawable parent) {
        this.parent = parent;

//        bmAdd = BitmapFactory.decodeResource(resources,R.drawable.btn_add);

        int frameColor = resources.getColor(R.color.fr_frame);
        int textColor = resources.getColor(R.color.fr_frame_text);
        float frameStroke = resources.getDimension(R.dimen.fr_frame_stroke);
        float textSize = resources.getDimension(R.dimen.fr_frame_banner_name);

        framePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        framePaint.setStyle(Paint.Style.STROKE);
        framePaint.setColor(frameColor);
        framePaint.setStrokeWidth(frameStroke);

        bannerBasePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bannerBasePaint.setStyle(Paint.Style.FILL);
        bannerBasePaint.setColor(frameColor);

        bannerTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bannerTextPaint.setColor(textColor);
        bannerTextPaint.setTextSize(textSize);

        bannerHt = resources.getDimension(R.dimen.fr_frame_banner_ht);
        textOffX = resources.getDimension(R.dimen.fr_frame_banner_offx);

        Rect bounds = new Rect();
        bannerTextPaint.getTextBounds("A", 0, 1, bounds);
        textY = - bounds.height();
    }

    /**
     * method for drawing the FR information
     *
     * @param cvs canvas
     */
    /* package */ void draw(Canvas cvs) {
        for (FaceFrame frame : frames) {
            RectF rect = frame.getRect();
            cvs.drawRect(rect.left, rect.top - (int) bannerHt,
                    rect.right, rect.top, bannerBasePaint);
            cvs.drawText(frame.getId(), rect.left + textOffX,
                    rect.top + textY, bannerTextPaint);
            cvs.drawRect(rect.left, rect.top - (int) bannerHt,
                    rect.right, rect.bottom, framePaint);
//            cvs.drawBitmap(bmAdd,rect.right,rect.bottom,bannerBasePaint);
        }
    }

    /**
     * sets the alpha for the frames UI
     *
     * @param alpha alpha value
     */
    /* package */ void setAlpha(int alpha) {
        framePaint.setAlpha(alpha);
        bannerBasePaint.setAlpha(alpha);
        bannerTextPaint.setAlpha(alpha);
    }

    /**
     * Method to set the color filter
     *
     * @param filter color filter
     */
    /* package */ void setColorFilter(ColorFilter filter) {
        framePaint.setColorFilter(filter);
        bannerBasePaint.setColorFilter(filter);
        bannerTextPaint.setColorFilter(filter);
    }

    /**
     * resets the frames
     */
    /* package */ void reset() {
        frames.clear();
    }

    /**
     * adds the fr frames
     *
     * @param newFrFrames image face frames
     * @param params      mapper params
     */
    public void addFrames(ArrayList<FaceFrame> newFrFrames, MapperParams params) {
        // Log.d(TAG, "got FR frames " + newFrFrames.size());
        ArrayList<FaceFrame> faceFrames = Utils.image2ScreenFaceFrames(newFrFrames, params);
        boolean changed = Algo.trackFaceFrames(frames, faceFrames, new Algo.FaceMatcher() {
            /**
             * {@inheritDoc}
             */
            @Override
            public boolean match(FaceFrame frame1, FaceFrame frame2) {
                return frame1.getId().equals(frame2.getId());
            }
        });
        if (changed) {
            parent.invalidateSelf();
        }
    }
}
